package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SelectStaffActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ListResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

public class SelectSingleStaffActivity extends BaseActivity {
    private SelectStaffActivityCustom dataBinding;
    private SlimAdapterEx<EmploymentListResultBean> slimAdapterEx;
    private int pageIndex = 0;
    private MVCCoolHelper<List<EmploymentListResultBean>> mvcHelper;
    private List<EmploymentListResultBean> selectedBean;
    private String selectedstr = "";

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_staff);
        initTopTitleBar(View.VISIBLE, "人员选择");
    }

    @Override
    public void initData() {
        selectedBean = getIntent().getParcelableArrayListExtra("list");
        if (selectedBean != null) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0, lenth = selectedBean.size(); i < lenth; i++) {
                stringBuilder.append(selectedBean.get(i).getGuid()).append(",");
            }
            selectedstr = stringBuilder.toString();
        }
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create()
                .register(R.layout.list_item_10, new SlimInjector<EmploymentListResultBean>() {
                    @Override
                    public void onInject(EmploymentListResultBean data, IViewInjector injector) {
                        injector.text(R.id.tv_upper_left_content, data.getStaffName())
                                .text(R.id.tv_upper_right_content, data.getDepartName())
                                .text(R.id.tv_down_right_content, data.getPostName())
                                .with(R.id.iv_head, v -> {
                                    ImageUtil.setCircleImage(data.getIphoto(), (ImageView) v);
                                })
                                .invisible(R.id.iv_enter);
                    }
                })
                .attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Intent intent = new Intent();
                    intent.putExtra(Config.CONTENT, slimAdapterEx.getDataItem(position).getStaffName());
                    intent.putExtra(Config.GUID, slimAdapterEx.getDataItem(position).getGuid());
                    intent.putExtra(Config.BEAN, (Serializable) slimAdapterEx.getDataItem(position));
                    setResult(Config.INTENT_CONTENT, intent);
                    finish();
                });
        dataBinding.floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
            @Override
            public void onSuggestionClicked(SearchSuggestion searchSuggestion) {

            }

            @Override
            public void onSearchAction(String currentQuery) {
                if (!TextUtils.isEmpty(currentQuery)) {
                    searchRequest(currentQuery);
                }
            }
        });
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        ModelRx2DataSource<EmploymentListResultBean> dataSource = new ModelRx2DataSource<>((page, register) -> OkGo.<HttpResult<ListResultBean<EmploymentListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.SELECT_EMPLOYMENT_ALL)
                .params("companyId", user.getCompanyId())
                .params("page", page == 1 ? 0 : pageIndex)
                .converter(new JsonConverter<HttpResult<ListResultBean<EmploymentListResultBean>>>() {
                    @Override
                    public void test() {
                        super.test();
                    }
                })
                .adapt(new FlowableBody<>())
                .flatMap(listResultBeanHttpResult -> {
                    pageIndex = listResultBeanHttpResult.getItems().getPage();
                    ArrayList<EmploymentListResultBean> result = new ArrayList<>();
                    if (!(listResultBeanHttpResult.getItems().getList() == null)) {
                        Flowable.fromIterable(listResultBeanHttpResult.getItems().getList())
                                .filter(employmentListResultBean -> !selectedstr.contains(employmentListResultBean.getGuid()))
                                .subscribe(employmentListResultBean -> result.add(employmentListResultBean));
                    }
                    return Flowable.just(result);
                }));
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
    }

    private void searchRequest(String currentQuery) {
        OkGo.<HttpResult<List<EmploymentListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.SEARCH_EMPLOY)
                .params("para", currentQuery)
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<List<EmploymentListResultBean>>>(SelectSingleStaffActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<EmploymentListResultBean>>> response) {
                        HttpResult<List<EmploymentListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            ArrayList<EmploymentListResultBean> result = new ArrayList<>();
                            if (!(httpResult.getItems() == null)) {
                                Flowable.fromIterable(httpResult.getItems())
                                        .filter(employmentListResultBean -> !selectedstr.contains(employmentListResultBean.getGuid()))
                                        .subscribe(employmentListResultBean -> result.add(employmentListResultBean));
                            }
                            slimAdapterEx.updateData(result);
                        }
                    }
                });
    }

    @Override
    public void setView() {
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
